<?php
namespace app\apps\controller;
use app\common\controller\Api;
class Vote extends Api {
    protected $mod = '';
    /**
     * @name 初始化方法
     */
    protected function _initialize(){
        $this->mod = model('apps/Vote');
    }
    /**
     * @name 未定的接口
     * @return array 
     */
    public function _empty(){
        return getApiErrorCode('00404');
    }
    /**
     * @name 发起投票
     */
    public function createVote(){
        $auth = self::checkApi();
        if($auth !== true) return $this->api_info;
        $res = $this->mod->createVote($this->data);
        return $res ? ['code'=>'00000','data'=>$res] : ['code'=>'90400','message'=>$this->mod->getError()];
    }
    /**
     * @name 获取指定用户发起的最后一次投票
     */
    public function getUserLastVote(){
        $auth = self::checkApi();
        if($auth !== true) return $this->api_info;
        if($this->usid){
            $vote = $this->mod->getVoteByUid(model('open/User')->getUidByUsid($this->usid));
        }else{
            $vote = $this->mod->getVoteByUid($this->uid);
        }
        return $vote ? ['code'=>'00000','data'=>$vote] : ['code'=>'91404','message'=>'未发起过投票'];
    }
    /**
     * @name 投票
     */
    public function poll(){
        $auth = self::checkApi(['vote_id','option_key','oper_token']);
        if($auth !== true) return $this->api_info;
        //加密验证
        $_token = md5($this->vote_id.$this->option_key);
        $t = strripos($this->oper_token,'n');
        $this->count = substr($this->oper_token,$t+1);
        $this->oper_token = substr($this->oper_token,0,$t);
        if(strtolower($_token) != strtolower($this->oper_token)){
            return ['code'=>'90401','message'=>'投票失败,请稍后再试'];
        }
        if($this->count && is_numeric($this->count)){
            $res = $this->mod->addAallot($this->vote_id,$this->option_key,$this->count,$this->uid);
            if($res !== true){
                return ['code'=>'90401','message' => $this->mod->getError()];
            }
        }
        return ['code'=>'00000','data'=>$this->mod->getVoteByVoteId($this->vote_id)];
        
    }
    /**
     * @name 根据投票标识查询投票信息
     */
    public function getVoteInfo(){
        $auth = self::checkApi(['vote_id']);
        if($auth !== true) return $this->api_info;
        $res = $this->mod->getVoteByVoteId($this->vote_id);
        if(!$res){
            return ['code'=>'90404','message' => '不存在该投票']; 
        }
        return ['code'=>'00000','data'=>$res];
    }
    /**
     * @name 提前结束投票
     */
    public function endVote(){
        $auth = self::checkApi(['vote_id']);
        if($auth !== true) return $this->api_info;
        $vote_id = $this->mod->getRealVoteId($this->vote_id);
        if($vote_id && $this->mod->isInitiator($this->uid,$vote_id) === true){
            $res = $this->mod->endVote($vote_id);
            if($res === true){
                return ['code' => '00000','data' => $this->mod->getVoteByVoteId($this->vote_id)];
            }
        }
        return ['code' => '90403','message' => '你没有权限操作'];
    }
    /**
     * @name 临时关闭投票
     */
    public function endVoteTemp(){
        $auth = self::checkApi(['vote_id']);
        if($auth !== true) return $this->api_info;
        $vote_id = $this->mod->getRealVoteId($this->vote_id);
        if($vote_id && $this->mod->isInitiator($this->uid,$vote_id) === true){
            $res = $this->mod->endVoteTemp($vote_id);
            if($res === true){
                return ['code' => '00000','data' => $this->mod->getVoteByVoteId($this->vote_id)];
            }else{
                return ['code'=>'90401','message'=>$this->mod->getError()];
            }
        }
        return ['code' => '90403','message' => '你没有权限操作'];
    }
    /**
     * @name 恢复投票
     */
    public function startVoteTemp(){
        $auth = self::checkApi(['vote_id']);
        if($auth !== true) return $this->api_info;
        $vote_id = $this->mod->getRealVoteId($this->vote_id);
        if($vote_id && $this->mod->isInitiator($this->uid,$vote_id) === true){
            $res = $this->mod->startVoteTemp($vote_id);
            if($res === true){
                return ['code' => '00000','data' => $this->mod->getVoteByVoteId($this->vote_id)];
            }else{
                return ['code'=>'90401','message'=>$this->mod->getError()];
            }
        }
        return ['code' => '90403','message' => '你没有权限操作'];
    }
    /**
     * @name 设置投票时间
     */
    public function setTime(){
        $auth = self::checkApi(['vote_id','time']);
        if($auth !== true) return $this->api_info;
        $vote_id = $this->mod->getRealVoteId($this->vote_id);
        if($vote_id && $this->mod->isInitiator($this->uid,$vote_id) === true){
            if(is_numeric($this->time)){
                $res = $this->mod->setVoteTime($vote_id,$this->time);
                if($res === false){
                    return ['code'=>'90401','message'=>$this->mod->getError()]; 
                }
            }
            return ['code' => '00000','data' => $this->mod->getVoteByVoteId($this->vote_id)]; 
        }
        return ['code' => '90403','message' => '你没有权限操作'];
        
    }
    
}